<template>
  <div class="login">
    <!-- 导航栏
    van-nav-bar
        title:中间标题
        left-text:左边文本
        right-text:右边文本
        left-arrow 左边剪头   boolean  true:显示左边剪头  false不显示
        @click-left:左边点击
        @click-right:右边点击
     -->
    <!-- title="登陆"
     left-text="左边"
     right-text="右边"
     :left-arrow="true"
    slot="title/left/right"

    -->
    <!-- 具名插槽
    定义 xxx组件
     <div>
      <slot name="title"></slot>
     </div>
    使用:
      <xxx>
          <template #title>
              自定义title
          </template>
      </xxx>

    -->
    <van-nav-bar>
      <template #left>
        <i class="iconfont f40">&#xe637;</i>
      </template>
    </van-nav-bar>
    <div class="content">
      <h3 class="title">您好,请登录</h3>
      <van-form ref="form">
        <van-field
          v-model="form.mobile"
          name="mobile"
          placeholder="请输入手机号"
          :rules="rules.mobile"
        >
          <template #left-icon>
            <i class="iconfont">&#xe60b;</i>
          </template>
        </van-field>
        <van-field
          v-model="form.code"
          placeholder="请输入验证码"
          :rules="rules.code"
        >
          <template #left-icon>
            <i class="iconfont">&#xe60c;</i>
          </template>
          <template #button>
            <span class="code" @click="getCode" v-if="totalTime === 6"
              >获取验证码
            </span>
            <span class="code" v-else>{{ totalTime }}S后重试</span>
          </template>
        </van-field>
      </van-form>
      <div class="tip">
        登录及同意<span class="color">《用户使用协议》</span>和<span
          class="color"
          >《隐私协议》</span
        >
      </div>
      <van-button block round color="#e40137" @click="submit">确定</van-button>
    </div>
  </div>
</template>

<script>
import { auCode, auLogin } from '@/api/login.js'
//导入
import { setLocal, getLocal } from '@/utils/local.js'
export default {
  name: 'Login',
  data () {
    return {
      form: {
        mobile: '',
        code: ''
      },
      totalTime: 6, //定义一个计时器 定义在return里
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '请输入正确的手机号',
            trigger: 'onChange'
          }
        ],
        code: [{ required: true, message: '请输入验证码', trigger: 'onChange' }]
      }
    }
  },
  created () {
    window.console.log(process.env.VUE_APP_URL)
    //如果有token就直接跳转到home
    if (getLocal('token')) {
      this.$router.push('/home/my')
    }
  },
  methods: {
    getCode () {
      this.$refs.form
        .validate('mobile')
        .then(() => {
          /* ({
            method: 'post',
            url: '/au/code',
            data: {
              mobile: this.form.mobile
            }
          }) */
          //成功后 调用计时器
          this.totalTime-- //加一个这个为了防止验证码按钮可以乱点
          const _interval = setInterval(() => {
            this.totalTime--
            if (this.totalTime <= 0) {
              //清楚计时器
              clearInterval(_interval)
              //还原计时器时间
              this.totalTime = 6
            }
          }, 1000)
          //成功后 调用验证码
          auCode({ mobile: this.form.mobile })
            .then(res => {
              /* window.console.log('----res', res) */
              this.$toast.success(res.data.data)
              this.form.code = res.data.data
            })
            .catch(error => {
              window.console.log('----error', error)
            })
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    },
    submit () {
      this.$refs.form
        .validate()
        .then(() => {
          /*this.$toast.success('验证成功') */
          auLogin({ mobile: this.form.mobile, code: this.form.code })
            .then(res => {
              this.$toast(res.data.message)
              console.log('进入then后:', res)
              //保存token
              setLocal('token', res.data.data.jwt)
              //设置用户名 store 是仓库
              this.$store.commit('setUserInfo', res.data.data.user)
              //设置登录状态为ture
              this.$store.commit('setIsLogin', true)
              //跳转页面
              this.$router.push('/home/my')
            })
            .catch(err => {
              this.$toast.error('登录错误', err)
            })
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  .f40 {
    font-size: 44px;
  }
  .content {
    padding: 0 @p15;
    .title {
      margin-top: 50px;
      margin-bottom: 63px;
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
    }
    .code {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 22px;
      letter-spacing: 0px;
    }
    .tip {
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16px;
      letter-spacing: 0px;
      margin-bottom: 44px;
      .color {
        color: #00b8d4;
      }
    }
  }
}
</style>

<style lang="less">
.login {
  .van-nav-bar__left,
  .van-nav-bar__right {
    padding: 0;
  }
}
</style>
